<div class="layui-card" id="categoryManage">
    <div class="layui-card-header">
        <h2 class="header-title">分类专栏</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>分类专栏</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <form class="layui-form toolbar" name="queryForm">
            &nbsp;&nbsp;
            <div class="layui-inline">
                创建时间
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="startDate" class="layui-input" id="startDate" placeholder="开始时间">
                </div>
                <!--<div class="layui-form-mid">-</div>-->
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="endDate" class="layui-input" id="endDate" placeholder="结束时间">
                </div>
            </div>
            &nbsp;&nbsp;
            专栏名称：
            <input name="caname" class="layui-input search-input enterSubmit" type="text"
                   placeholder="请输入关键字"/>&emsp;
            <button id="category-btn-search" class="layui-btn icon-btn" lay-submit lay-filter="categoryManage-query"><i
                    class="layui-icon">&#xe615;</i>搜索
            </button>

            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button id="category-btn-add" type="button" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </form>

        <!-- 数据表格 -->
        <table class="layui-table" id="category-table" lay-filter="category-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="category-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['form', 'table', 'util', 'laydate', 'config', 'admin'], function () {
        let form = layui.form,
            table = layui.table,
            config = layui.config,
            layer = layui.layer,
            util = layui.util,
            admin = layui.admin,
            laydate = layui.laydate;
        //日期时间范围
        laydate.render({
            elem: '#categoryManage #startDate'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#categoryManage #endDate'
            , type: 'datetime'
        });


        let userid = config.getUser().id;
        let url = config.base_server + 'api-forum/article/category/page?userid='.concat(userid);
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'cateid', sort: true, hide: true, title: 'ID', width: 100},
            {field: 'caname', sort: true, title: '专栏名称', align: 'center'},
            {field: 'cainfo', sort: true, title: '专栏简介', align: 'center'},
            {
                field: 'banner', sort: true, title: '专栏配图', align: 'center', templet: (d) => {
                    return '<img src="' + (d.banner ? d.banner : '../../assets/images/category.png') + '" alt="" class="layui-badge layui-bg-gray"/>'
                }
            },
            {field: 'casort', sort: true, title: '排序', align: 'center'},
            {
                field: 'cretim', sort: true, align: 'center', templet: (d) => {
                    return util.toDateString(d.cretim);
                }, title: '创建时间'
            },
            {
                field: 'updtim', sort: true, align: 'center', templet: (d) => {
                    return util.toDateString(d.updtim);
                }, title: '修改时间'
            },
            {templet: '#category-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#category-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(category-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-forum/article/category/' + obj.data.cateid, {}, (data) => {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('category-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');
                });
            }
        });

        // 添加按钮点击事件
        $('#category-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            admin.putTempData('t_category', data);
            let title = data ? '编辑分类专栏' : '新建分类专栏';
            admin.popupCenter({
                title: title,
                path: 'pages/forum/category_form.html',
                area: '800px',
                offset: '200px',
                finish: () => {
                    admin.putTempData('t_category', null);
                    table.reload('category-table', {});
                },
                cancel: () => {
                    admin.putTempData('t_category', null);
                }
            });
        };

        // 条件检索
        form.on('submit(categoryManage-query)', (data) => {
            table.reload('category-table', {where: data.field});
            return false;
        });


        // 回车查询
        $('.enterSubmit').keyup((e) => {
            switch (e.keyCode) {
                case 13:
                    $('#category-btn-search').click();
                    break;
            }
        });
    });
</script>